package orm;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public abstract class SetDBHelper {

	
	/**
	 * If Set id == 0 new exercise will be created in the db
	 * the row ID of the newly inserted row, or -1 if an error occurred
	 * @param context
	 * @param exercise
	 * @return
	 */
	public static long saveSet(Context context, Set set){
		if(set.getUnit_id() == 0){
			return addSet(context, set);
		}
		return 111;
		
	}
	
	/**
	 * 
	 * @param context
	 * @param unit
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public static long addSet(Context context, Set set) {
		
		DBHelper mDbHelper = new DBHelper(context);
		SQLiteDatabase db = mDbHelper.getWritableDatabase();

		long id = db.insert(DBContract.Set.TABLE_NAME,
				DBContract.Set.COLUMN_NAME_NULLABLE, getAllValues(set));

		mDbHelper.close();
		return id;
		

	}
	
	
	/**
	 * 
	 * @param context
	 * @param id
	 * @return null if no Unit with the given id is found
	 */
	public static Set getSetByID(Context context, long id) {
		DBHelper mDbHelper = new DBHelper(context);
		SQLiteDatabase db = mDbHelper.getReadableDatabase();

		String selection = DBContract.Set.COLUMN_UNIT_ID + " = "+ id;

		Cursor c = db.query(DBContract.Set.TABLE_NAME, // The table to query
				getAllProjectionCols(), // The columns to return
				selection, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				null // The sort order
				);

		Date date;
		SimpleDateFormat iso8601Format = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		if (c.moveToFirst()) {

			try {
				date = iso8601Format.parse(c.getString(3));
			} catch (java.text.ParseException e) {
				date = null;
				e.printStackTrace();
				mDbHelper.close();
			}
			mDbHelper.close();
			return new Set(
					Integer.parseInt(c.getString(0)),
					Integer.parseInt(c.getString(1)), 
					Integer.parseInt(c.getString(2)), 
					date,
					Float.parseFloat(c.getString(4)),
					Integer.parseInt(c.getString(5))
					);
		}
		
		mDbHelper.close();
		if (c != null && ! c.isClosed()) {
			c.close();
		}
		if (db != null) {
			db.close();
		}

		return null;
	}
	
	
	/**
	 * ContentValues: This class is used to store a set of values that the ContentResolver can process.
	 * @param Set
	 * @return a map of all values/columns, where column names are the keys
	 */
	private static ContentValues getAllValues(Set set){
		
		ContentValues values = new ContentValues();
	    values.put(DBContract.Set.COLUMN_UNIT_NR, set.getUnitNr());
	    values.put(DBContract.Set.COLUMN_VALUE_INT, set.getValueInt());
	    values.put(DBContract.Set.COLUMN_VALUE_TIME, set.getValueTime().toString());
	    values.put(DBContract.Set.COLUMN_VALUE_WEIGHT, set.getValueWeight());
	    values.put(DBContract.Set.COLUMN_EXERCISE_FK, set.getExercise_fk());
		
		return values;
	}

	/**
	 * Defines a projection that specifies which columns from the database you
	 * will actually use after this query.
	 * 
	 * @return
	 */
	private static String[] getAllProjectionCols() {

		String[] projection = {
			    DBContract.Set.COLUMN_UNIT_ID,
			    DBContract.Set.COLUMN_UNIT_NR,
			    DBContract.Set.COLUMN_VALUE_INT,
			    DBContract.Set.COLUMN_VALUE_TIME,
			    DBContract.Set.COLUMN_VALUE_WEIGHT,
			    DBContract.Set.COLUMN_EXERCISE_FK
				};

		return projection;
	}
	
	

}
